﻿using System;
using System.ComponentModel.Design;
using System.Windows.Forms;

using Microsoft.WowAddonStudio.FrameXml.Components;
using Microsoft.WowAddonStudio.FrameXml.Design;
using Microsoft.WowAddonStudio.FrameXml.Design.Serialization;

namespace Microsoft.WowAddonStudio.FrameXml.Designer
{
    public partial class FrameXmlForm : Form
    {
        private readonly string frameXmlPath;

        public FrameXmlForm(string frameXmlPath)
        {
            InitializeComponent();

            this.frameXmlPath = frameXmlPath;
        }

        public FrameXmlForm()
            : this(null)
        {
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            InitializeDesignSurface();
        }

        private void InitializeDesignSurface()
        {
            var designerFactory = ((FrameXmlDesignerForm)MdiParent).GetService<FrameXmlDesignerFactory>();

			DesignSurface designSurface = designerFactory.CreateDesignSurface();
            
            if (frameXmlPath != null)
                designSurface.BeginLoad(new FrameXmlDesignerLoader(new FileFrameXmlSerializer(frameXmlPath)));
            else
                designSurface.BeginLoad(typeof(UiControl));

            var designSurfaceView = (Control)designSurface.View;
            designSurfaceView.Dock = DockStyle.Fill;
            Controls.Add(designSurfaceView);
        }
    }
}
